/**
 * Copyright (c) 2011 eelpo.com
 * All rights reserved.
 */
package com.eelpo.cmmp.uac.services.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import com.eelpo.cmmp.core.exception.ServiceException;
import com.eelpo.cmmp.uac.dao.IRoleManageDao;
import com.eelpo.cmmp.uac.dao.IUserManageDao;
import com.eelpo.cmmp.uac.dao.entity.TRole;
import com.eelpo.cmmp.uac.dao.entity.TUser;
import com.eelpo.cmmp.uac.dao.vo.Role;
import com.eelpo.cmmp.uac.dao.vo.enumed.Status;
import com.eelpo.cmmp.uac.services.IRoleManageService;
import com.eelpo.common.util.BlankUtils;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * <pre>
 * 
 * 角色管理服务层具体实现
 * 
 * </pre>
 * 
 * @author <a href="mailto:Brian@eelpo.com">汪洪州</a>
 * @since 1.0
 */
@Service
public class RoleManageService implements IRoleManageService {

	@Resource
	protected IRoleManageDao roleManageDao;

	@Resource
	protected IUserManageDao userManageDao;

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.services.IRoleManageService#getRoleById(java.lang
	 *      .String)
	 */
	@Override
	public Role getRoleById(String id) throws ServiceException {
		TRole role = roleManageDao.getRoleById(id);

		if (BlankUtils.isBlank(role)) {
			throw new ServiceException("查询的角色不存在！");
		}
		return po2vo(role);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.services.IRoleManageService#checkExist(java.lang.
	 *      String)
	 */
	@Override
	public boolean checkExist(String name) throws ServiceException {
		return roleManageDao.checkExist(name);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.services.IRoleManageService#addRole(com.eelpo.cmmp
	 *      .uac.dao.vo.Role)
	 */
	@Override
	public String addRole(Role role) throws ServiceException {

		// 检查实体类型是否已经存在
		if (roleManageDao.checkExist(role.getName())) {
			throw new ServiceException("角色[" + role.getName() + "]已经存在！");
		}

		role.setAddTime(new Date());

		return roleManageDao.addRole(vo2po(role));
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.services.IRoleManageService#updateRole(com.eelpo
	 *      .cmmp.uac.dao.vo.Role)
	 */
	@Override
	public void updateRole(Role role) throws ServiceException {

		// 检查实体类型是否已经存在
		if (roleManageDao.checkExistByUpdate(role.getId(), role.getName())) {
			throw new ServiceException("角色[" + role.getName() + "]已经存在！");
		}

		// 获取角色
		TRole trole = roleManageDao.getRoleById(role.getId());

		// 更新角色的非关系数据
		roleManageDao.updateRole(vo2po(trole, role));
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.services.IRoleManageService#disabledRole(com.eelpo.cmmp.uac.dao.vo.Role)
	 */
	@Override
	public void disabledRole(Role role) throws ServiceException {

		TRole trole = roleManageDao.getRoleById(role.getId());

		trole.setStatus(Status.DISABLED.getValue());

		roleManageDao.updateRole(trole);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.services.IRoleManageService#enabledRole(com.eelpo.cmmp.uac.dao.vo.Role)
	 */
	@Override
	public void enabledRole(Role role) throws ServiceException {

		TRole trole = roleManageDao.getRoleById(role.getId());

		trole.setStatus(Status.ENABLED.getValue());

		roleManageDao.updateRole(trole);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.services.IRoleManageService#deleteRole(com.eelpo
	 *      .cmmp.uac.dao.vo.Role)
	 */
	@Override
	public void deleteRole(Role role) throws ServiceException {

		if (roleManageDao.checkRoleUsed(role.getId())) {
			throw new ServiceException("无法删除，该角色已经被使用！");
		}

		roleManageDao.deleteRole(role.getId());
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.services.IRoleManageService#getRolesPaing(java.lang.String,com.eelpo
	 *      .cmmp.uac.dao.vo.Role, int, int)
	 */
	@Override
	public List<Role> getRolesPaing(String userId, Role role, int first, int max) throws ServiceException {

		TUser tuser = userManageDao.getUserById(userId);

		if (BlankUtils.isBlank(tuser)) {
			throw new ServiceException("用户不存在！");
		}

		if (tuser.getSuperManager()) {
			return po2vo(roleManageDao.getRolesPaing(role, first, max));
		} else {
			return po2vo(roleManageDao.getRolesPaing(userId, role, first, max));
		}
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.services.IRoleManageService#getRolesCount(java.lang.String,com.eelpo
	 *      .cmmp.uac.dao.vo.Role)
	 */
	@Override
	public int getRolesCount(String userId, Role role) throws ServiceException {

		TUser tuser = userManageDao.getUserById(userId);

		if (BlankUtils.isBlank(tuser)) {
			throw new ServiceException("用户不存在！");
		}

		if (tuser.getSuperManager()) {
			return roleManageDao.getRolesCount(role);
		} else {
			return roleManageDao.getRolesCount(userId, role);
		}
	}

	/**
	 * 静态转换，不包含任何关联对象的转换
	 * 
	 * @param role
	 *            角色值对象
	 * @return 返回角色实体对象
	 */
	public static TRole vo2po(Role role) {
		return vo2po(new TRole(), role);
	}

	/**
	 * 静态转换，不包含任何关联对象的转换
	 * 
	 * @param role
	 *            角色值对象
	 * @return 返回角色实体对象
	 */
	public static TRole vo2po(TRole trole, Role role) {

		BeanUtils.copyProperties(role, trole, new String[] { "status" });

		trole.setStatus(Status.valueOf(role.getStatus()));

		return trole;
	}

	/**
	 * 静态转换，不包含任何关联对象的转换
	 * 
	 * @param trole
	 *            角色实体对象
	 * @return 返回角色值对象
	 */
	public static Role po2vo(TRole trole) {
		Role role = new Role();
		BeanUtils.copyProperties(trole, role, new String[] { "status" });
		role.setStatus(Status.valueOf(trole.getStatus()));
		return role;
	}

	/**
	 * 静态转换，不包含任何关联对象的转换
	 * 
	 * @param troles
	 *            角色实体对象列表
	 * @return 返回角色值对象列表
	 */
	public static List<Role> po2vo(List<TRole> troles) {
		List<Role> roles = new ArrayList<Role>();
		for (TRole trole : troles) {
			roles.add(po2vo(trole));
		}
		return roles;
	}
}
